import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt


#载入图像
img = cv.imread('car.png')
#可以通过像素值的行和列坐标来访问它。对于BGR图像，它将返回一个蓝色，绿色，红色值的数组。对于灰度图像，仅返回相应的强度
blue = img[100,100,0]
print(blue)   #206
print(img[100,100])  # [206 197 183]
img = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
print(img[100,100])  #194
#像素总数
print(img.size)  #34100
print(img.shape)  #(155, 220)
#访问red的值
img = cv.imread('car.png')
red = img.item(100,100,2)
print(red)  #183
#修改red的值
img.itemset((100,100,2),100)
print(img.item(100,100,2)) #100
#图片数据类型
print(img.dtype)#uint8

#复制
img = cv.imread('car.png')
img1 = img[0:50,0:50]
img[50:100,0:50] = img1
cv.imshow('img',img)
cv.waitKey(0)

#分割和合并图像通道
b,g,r = cv.split(img)
img = cv.merge((b,g,r))

#cv.copyMakeBorder（）创建画框
BLUE = [255,0,0]
img1 = cv.imread('car.png')
replicate = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REPLICATE)
reflect = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT)
reflect101 = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT_101)
wrap = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_WRAP)
constant= cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()